package com.lanou.dllo.vc.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.easemob.chat.EMConversation;
import com.lanou.dllo.vc.R;
import com.lanou.dllo.vc.activity.ChatActivity;
import com.lanou.dllo.vc.greendao.ChatMessage;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by dllo on 15/12/8.
 */
public class NewsListAdapter extends BaseAdapter {
    private List<EMConversation> datas;
    private LayoutInflater inflater;
    private Context context;

    public NewsListAdapter(Context c) {
        this.context = c;
        inflater = LayoutInflater.from(c);
    }

    public void addData(List<EMConversation> conversationList) {
        this.datas = conversationList;
    }

    @Override
    public int getCount() {
        return datas != null && datas.size() > 0 ? datas.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.news_list_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        EMConversation data = datas.get(position);

        if (data!=null) {
            String message = data.getLastMessage().getBody().toString();
            if (message.startsWith("txt")) {
                if (message.contains("ee_")) {//表情
                    holder.messageTv.setText("[表情]");
                }else {//文字
                    holder.messageTv.setText(message.substring(5,message.length()-1));
                }
            }
            if (message.startsWith("http://")) {//图片
                holder.messageTv.setText("[图片]");
            }
            if (message.startsWith("voice")) {//语音消息
                holder.messageTv.setText("[语音]");
            }

            holder.from_usernameTv.setText(data.getUserName());
        }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, ChatActivity.class);
                    intent.putExtra("name", datas.get(position).getUserName());
                    context.startActivity(intent);
                }
            });

        return convertView;
    }

    class ViewHolder {
        @Bind(R.id.from_imgIv)
        ImageView from_imgIv;
        @Bind(R.id.from_usernamaTv)
        TextView from_usernameTv;
        @Bind(R.id.messageTv)
        TextView messageTv;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
